package com.zxjbyte.yiyi.framework.jackson.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.jackson.module.YiyiJavaTimeModule;
import com.zxjbyte.yiyi.framework.jackson.module.YiyiNumberModule;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 全局处理时间类型序列化，及LONG类型精度
 * @Author zhangxingjia
 * @Date 2022/9/12 19:04
 * @Version: 1.0
 */
@ConditionalOnClass(ObjectMapper.class)
@AutoConfiguration(before = JacksonAutoConfiguration.class)
public class YiyiJacksonAutoConfiguration {

    /**
     * 自定义ObjectMapper配置
     * @return
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return builder -> {
            //设置地点为中国
            builder.locale(Locale.CHINA);
            //设置为中国上海时区
            builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
            // 影响全局Date类型序列化时时间格式 如果定制化处理使用 @JsonFormat
            builder.simpleDateFormat(Constant.PATTERN_DATETIME);
            // 处理java8时间类型序列化时事件格式
            builder.modules(YiyiJavaTimeModule.INSTANCE, YiyiNumberModule.INSTANCE);
        };
    }

}
